import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class Show {
    private static List<Show> allShows = new ArrayList<>(); // 全局场次列表
    private String showId;
    private Movie movie;
    private Hall hall;
    private Date showTime;
    private double price;
    private int remainingSeats;

    public Show(String showId, Movie movie, Hall hall, Date showTime, double price) {
        this.showId = (showId == null || showId.trim().isEmpty()) ? "SHOW_" + UUID.randomUUID().toString().substring(0, 6) : showId;
        this.movie = movie;
        this.hall = hall;
        this.showTime = showTime;
        this.price = price;
        this.remainingSeats = hall.getTotalSeatsCount();
    }

    public static List<Show> getAllShows() {
        return new ArrayList<>(allShows);
    }

    public static void addToAllShows(Show show) {
        if (show != null && !allShows.contains(show)) {
            allShows.add(show);
        }
    }

    public static void removeFromAllShows(Show show) {
        allShows.remove(show);
    }

    public static Show findShowById(String showId) {
        for (Show show : allShows) {
            if (show.getShowId().equals(showId)) {
                return show;
            }
        }
        return null;
    }

    public Date getEndTime() {
        if (movie == null || showTime == null) {
            return null;
        }
        long durationMillis = movie.getDuration() * 60 * 1000L;
        return new Date(showTime.getTime() + durationMillis);
    }

    public boolean isExpired() {
        Date endTime = getEndTime();
        return endTime != null && endTime.before(new Date());
    }

    public boolean isTimeConflict(Show otherShow) {
        if (otherShow == null || !this.hall.getHallName().equals(otherShow.hall.getHallName())) {
            return false;
        }
        Date thisStart = this.showTime;
        Date thisEnd = this.getEndTime();
        Date otherStart = otherShow.showTime;
        Date otherEnd = otherShow.getEndTime();

        if (thisStart == null || thisEnd == null || otherStart == null || otherEnd == null) {
            return false;
        }
        return !(thisEnd.before(otherStart) || thisStart.after(otherEnd));
    }

    public String getShowId() {
        return showId;
    }

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }

    public Hall getHall() {
        return hall;
    }

    public void setHall(Hall hall) {
        this.hall = hall;
    }

    public Date getShowTime() {
        return showTime;
    }

    public String getFormattedShowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(showTime);
    }

    public void setShowTime(Date showTime) {
        this.showTime = showTime;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        if (price > 0) {
            this.price = price;
        } else {
            System.out.println("票价必须大于0");
        }
    }

    public int getRemainingSeats() {
        return remainingSeats;
    }

    public boolean reduceSeats(int num) {
        if (num > 0 && num <= remainingSeats) {
            remainingSeats -= num;
            return true;
        }
        return false;
    }

    public boolean addSeats(int num) {
        if (num > 0 && (remainingSeats + num) <= hall.getTotalSeatsCount()) {
            remainingSeats += num;
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return String.format("场次ID: %s\n影片: %s\n放映厅: %s\n时间: %s\n票价: %.2f元\n剩余座位: %d", showId, movie != null ? movie.getName() : "未知影片", hall != null ? hall.getHallName() : "未知放映厅", showTime != null ? sdf.format(showTime) : "未知时间", price, remainingSeats);
    }
}